package cn.edu.sanxiau.www.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import cn.edu.sanxiau.www.dao.RolePermissionDao;
import cn.edu.sanxiau.www.model.RolePermission;
import frame.DBUtils.JdbcUtils;

public class RolePermissionDaoImpl implements RolePermissionDao {

    @Override
    public int addRolePermissionByRolePermission(RolePermission rolePermission) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JdbcUtils.getConnection();
            String sql = "INSERT INTO  rolepermission (role_permission_id,role_id,permission_id) VALUES(?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, rolePermission.getRole_permission_id());
            pstmt.setInt(2, rolePermission.getRole_id());
            pstmt.setInt(3, rolePermission.getPermission_id());

            int n = pstmt.executeUpdate();
            return n;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int deleteRolePermissionByRole_id(int role_id) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = JdbcUtils.getConnection();

            String sql = "DELETE FROM rolepermission WHERE role_id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, role_id);

            int m = pstmt.executeUpdate();
            return m;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

}
